﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using System.Globalization;


namespace Jy123.Web.Module.Venues
{
    public class VenuesHire1 : IHttpHandler
    {
        private int vId;
        private int hId;
        NumberFormatInfo nInfo = new NumberFormatInfo();
        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.QueryString["Type"];
            string strVId = context.Request.QueryString["VenuesId"];
            string hireId = context.Request.QueryString["HireId"];
            nInfo.CurrencyDecimalDigits = 2;
            if (!string.IsNullOrEmpty(strVId))
            {
                vId = int.Parse(strVId);

                if (!string.IsNullOrEmpty(type))
                {
                    Jy123.BLL.Public_Hire BLLPHire = new Jy123.BLL.Public_Hire();
                    Jy123.BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                    Jy123.BLL.Public_HireRecord BLLHireRecord = new Jy123.BLL.Public_HireRecord();
                    Jy123.BLL.Hire.Hire BLLHire = new Jy123.BLL.Hire.Hire();
                    Jy123.Model.Module_Main module = BLLModule.GetModel(vId);
                    Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                    Jy123.Model.Venues_Main venues = BLLVenues.GetModel(vId);
                    Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                    Jy123.Model.Club_Main club = BLLClub.GetModel(vId);
                    Jy123.BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
                    Jy123.Model.TCenter_Main TCenter = BLLTCenter.GetModel(vId);

                    Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                    Jy123.Model.Public_JType pJType = BLLJType.GetModel(module.FJType.Value);
                    int jFather = pJType.JFather.Value;
                    bool isVenues = false;
                    bool isTCenter = false;
                    if (jFather == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.Venues_Main))
                    {
                        isVenues = true;
                    }
                    if (jFather == Convert.ToInt32(Jy123.BLL.Public.EnumConfig.JyType.TCenter_Main))
                    {
                        isTCenter = true;
                    }
                    string msg = string.Empty;

                    switch (type.ToLower())
                    {
                        case "hire":
                            hId = int.Parse(hireId);
                            Jy123.Model.Public_Hire cPHire = BLLPHire.GetModel(hId);
                            Jy123.Model.Public_HireRecord hireRecord = new Jy123.Model.Public_HireRecord();
                            hireRecord.FHire = hId;
                            hireRecord.FModule = vId;
                            hireRecord.StartTime = DateTime.Now;
                            hireRecord.Status = 1;

                            msg = "已经开始租用，租用项目为" + cPHire.HName;
                            if (isVenues)
                            {
                                hireRecord.ModuleName = venues.VName;
                                //扣除赛场消耗的耐久度
                                BLL.Public_Consume BLLConsume = new BLL.Public_Consume();
                                List<Model.Public_Consume> listConsume = BLLConsume.GetModelList("FJType = " + venues.FJType.Value.ToString());
                                if (listConsume.Count > 0)
                                {
                                    Model.Public_Consume consume = listConsume[0];
                                    venues.Vitality = venues.Vitality.Value - consume.RentScore.Value;
                                    if (venues.Vitality.Value < 0)
                                    {
                                        venues.Vitality = 0;
                                    }
                                }
                                venues.VStatus = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.VenuesStatus.Hire);
                                BLLVenues.Update(venues);
                            }
                            else if (isTCenter)
                            {
                                hireRecord.ModuleName = TCenter.TName;
                                //扣除赛场消耗的耐久度
                                BLL.Public_Consume BLLConsume = new BLL.Public_Consume();
                                List<Model.Public_Consume> listConsume = BLLConsume.GetModelList("FJType = " + module.FJType.Value.ToString());
                                if (listConsume.Count > 0)
                                {
                                    Model.Public_Consume consume = listConsume[0];
                                    TCenter.Vitality = TCenter.Vitality.Value - consume.RentScore.Value;
                                    if (TCenter.Vitality.Value < 0)
                                    {
                                        TCenter.Vitality = 0;
                                    }
                                }
                                TCenter.TStatus = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.TCenterStatus.Hire);
                                BLLTCenter.Update(TCenter);
                            }
                            else
                            {
                                hireRecord.ModuleName = club.CName;
                                club.CStatus = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.ClubStatus.Hire);
                                BLLClub.Update(club);
                            }
                            BLLHireRecord.Add(hireRecord);
                            module.MStatus = 4;
                            BLLModule.Update(module);
                            break;
                        case "load":
                            Model.Public_HireRecord lRecord = BLLHire.GetLatestLiveRecord(vId);
                            if (lRecord != null)
                            {
                                Jy123.Model.Public_Hire pHire = BLLPHire.GetModel(lRecord.FHire.Value);
                                DateTime startTime = lRecord.StartTime.Value;
                                DateTime now = DateTime.Now;
                                decimal duration = (now - startTime).Hours + Convert.ToDecimal((now - startTime).Minutes) / 60;
                                if (duration >= pHire.Duration.Value)
                                {
                                    CalculateMoney(lRecord, module, club, venues, TCenter, isVenues, isTCenter);
                                }
                            }

                            break;
                        case "cancel":
                            Model.Public_HireRecord cRecord = BLLHire.GetLatestLiveRecord(vId);
                            if (cRecord != null)
                            {
                                DateTime startTime = cRecord.StartTime.Value;
                                DateTime now = DateTime.Now;
                                decimal duration = (now - startTime).Hours + Convert.ToDecimal((now - startTime).Minutes) / 60;
                                Jy123.Model.Public_Hire cCHire = BLLPHire.GetModel(cRecord.FHire.Value);
                                decimal cash = duration * cCHire.Price.Value;
                                msg = "已经取消租用，已经租用" + (now - startTime).Hours.ToString() + "小时" + (now - startTime).Minutes.ToString() + "分钟，收益" + Convert.ToInt32(cash).ToString();
                                CalculateMoney(cRecord, module, club, venues, TCenter, isVenues, isTCenter);
                            }

                            break;
                    }
                    BindHtml(context);
                    if (type.ToLower().Equals("cancel"))
                    {
                        context.Response.Write("<script>$.moonbox.ok('" + msg + "')</script>");
                    }
                    else if (type.ToLower().Equals("hire"))
                    {
                        context.Response.Write("<script>$.moonbox.ok('" + msg + "')</script>");
                    }
                }
            }
        }

        private void CalculateMoney(Model.Public_HireRecord record, Model.Module_Main module, Model.Club_Main club, Model.Venues_Main venues, Model.TCenter_Main TCenter, bool isVenues, bool isTCenter)
        {
            if (record != null)
            {
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
                BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                BLL.Public_Hire BLLPHire = new Jy123.BLL.Public_Hire();
                BLL.Public_HireRecord BLLHireRecord = new Jy123.BLL.Public_HireRecord();
                Jy123.Model.Public_Hire pHire = BLLPHire.GetModel(record.FHire.Value);
                Jy123.Model.User_Main user = BLLUser.GetModel(module.FUser.Value);
                DateTime startTime = record.StartTime.Value;
                DateTime now = DateTime.Now;
                int duration = (now - startTime).Hours;

                user.Cash = Convert.ToInt32(user.Cash.Value + duration * (pHire.Price.Value));
                BLLUser.Update(user);
                record.Status = 0;//0代表已经结算，需要改进
                BLLHireRecord.Update(record);
                if (isVenues)
                {
                    venues.VStatus = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.VenuesStatus.Empty);
                    BLLVenues.Update(venues);
                }
                else if (isTCenter)
                {
                    TCenter.TStatus = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.TCenterStatus.Empty);
                    BLLTCenter.Update(TCenter);
                }
                else
                {
                    club.CStatus = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.ClubStatus.Empty);
                    BLLClub.Update(club);
                }
                module.MStatus = 0;
                BLLModule.Update(module);
            }
        }

        private void BindHtml(HttpContext context)
        {
            Jy123.BLL.Public_Hire BLLPHire = new Jy123.BLL.Public_Hire();
            Jy123.BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
            Jy123.BLL.Public_HireRecord BLLHireRecord = new Jy123.BLL.Public_HireRecord();
            Jy123.BLL.Hire.Hire BLLHire = new Jy123.BLL.Hire.Hire();
            Jy123.Model.Public_HireRecord record = BLLHire.GetLatestLiveRecord(vId);
            string html = context.Cache["venueshirediv"].ToString();
            string strClick1 = "onclick=\"Validate()\"";
            string strClick2 = "onclick=\"fillhtml('btnDiv','/Module/Venues/VenuesHire.ashx?Type=Cancel&VenuesId='+venuesHidden.value+'&'+Math.random())\"";
            string btn1 = "<input class='QC6ZuBtn1' id='button2' " + strClick1 + " type='button' />";
            string btn2 = "<input class='QC6ZuBtn2'  id='button3' " + strClick2 + " type='button' />";
            if (record != null)
            {
                Jy123.Model.Public_Hire pHire = BLLPHire.GetModel(record.FHire.Value);
                DateTime startTime = record.StartTime.Value;
                DateTime now = DateTime.Now;
                decimal duration = (now - startTime).Hours + Convert.ToDecimal((now - startTime).Minutes) / 60;
                if (duration < pHire.Duration.Value)
                {
                    decimal cash = duration * pHire.Price.Value;
                    html = html.Replace("$HireStatus$", "已经租用" + (now - startTime).Hours.ToString() + "小时" + (now - startTime).Minutes.ToString() + "分钟，收益" + (Convert.ToInt32(cash)).ToString() + "(租用项目为" + pHire.HName + ")");
                    html = html.Replace("$Btn$", btn2);
                    html = html.Replace("$HireTime$", pHire.Duration.Value.ToString() + "小时");
                }
            }

            html = html.Replace("$HireTime$", "未租用");
            html = html.Replace("$HireStatus$", "未租用");
            html = html.Replace("$Btn$", btn1);

            context.Response.Write(html);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
